;(function($){
	$.fn.spa_jz_type = function(opts){
		var el = this;
		var opt = {
			no : "",
			mno : "",
			netamt : "",
			width : 100,
			rNum : 4,
			lNum : 4,
			sh : 38,//smallHeight
			bh : 48,//bigHeigtht  
			ds : [],
			price : "",
			initDs : [],
			brzd_array : [],
			pageTh : "",
			bfs : 16,
			sfs : 14,
			diffStyleSize : 1400,
			operation:"",		//结账类型
			xhs:"",
			winWidth : $(window).width(),
			dataBack : function(){},
			closeBack : function(){},
			printBillBack : function(){},
			refreshBack : function(){}, 
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		
		this.initFrameHtml();
		this.bindEvent();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//其他结帐方式
			el.find("#btnOther").unbind("click").click(function(){
				var json = param.getConnectInfo();
				param.togatherPrice = json.togatherPrice;
				param.nos = json.nos;
				param.nos_checkout = json.nos_checkout;
				$(this).modalFrame("destroy");
				$(this).modalFrame("init",function(modalEle){   
					$("#"+modalEle).modal("show"); 
					if(!$.isFunction($().spa_payment_other)){
						$.ajaxSetup({ async: false, cache: false });
			            $.getScript("js/cashier_mana/jquery.modal.spa_payment_other.js");
			            $.ajaxSetup({ async: true });
					}
					$("#"+modalEle).spa_payment_other({
						no : param.no,
					    price:param.price,//支付价钱
					    bigType : param.bigType,
						netamt : param.netamt,
						mno : param.mno,
						price : param.price,
						togatherPrice:param.togatherPrice,
						nos : param.nos,
						nos_checkout:param.nos_checkout,
					    ds : param.initDs,
					    operation:param.operation,
					    xhs:param.xhs,
					    printBillBack : function(data){
					    	param.printBillBack();
					    	$(".modal-backdrop.fade.in").removeClass("in").remove();
					    	t.printBill(data);
					    },
						dataBack : function(){
							param.dataBack();
						},
						closeBack : function(){
							param.closeBack();
						},
					});
				});
			});
			
			//结帐功能---》新
			el.find(".paymenttype").unbind("click").click(function(){
				var title = $(this).text();//.replace("支付","") + "支付";
				var bh = $(this).attr("data-code");
				var bhText = $(this).attr("data-text");
				var mc = $(this).text();
				var json = param.getConnectInfo();
				param.togatherPrice = json.togatherPrice;
				param.nos = json.nos;
				param.nos_checkout = json.nos_checkout;
				if($(this).attr("data-paytype") == "WC"){//微信支付
					$(this).modalFrame("destroy");
					$(this).modalFrame("init",function(modalEle){
						$("#"+modalEle).modal("show"); 
						if(!$.isFunction($().spa_wechatAlipayPay)){
							$.ajaxSetup({ async: false, cache: false });
				            $.getScript("js/cashier_mana/jquery.modal.spa_wechatAlipayPay.js");
				            $.ajaxSetup({ async: true });
						}
						$("#"+modalEle).spa_wechatAlipayPay({
							title:title?title:"支付方式",//标题
						    price:param.price,//支付价钱
						    togatherPrice:param.togatherPrice,
						    nos : param.nos,
						    nos_checkout:param.nos_checkout,
						    bh : bh,
						    operation:param.operation,
						    xhs:param.xhs,
						    bhText : bhText,
							returnBack:function(){
								$("#"+modalEle).empty();
								$("#"+modalEle).modal("hide"); 
							},
							getDefaultBack:function(){
								$("#"+modalEle).empty();
								$("#"+modalEle).modal("hide"); 
								param.printBillBack();
								t.printBill();
							}
						});
					});
				}else if($(this).attr("data-paytype") == "AR"){//AR账
					$(this).modalFrame("destroy");
					$(this).modalFrame("init",function(modalEle){   
						$("#"+modalEle).modal("show"); 
						if(!$.isFunction($().spa_ARAccountPay)){
							$.ajaxSetup({ async: false, cache: false });
				            $.getScript("js/cashier_mana/jquery.modal.spa_ARAccountPay.js");
				            $.ajaxSetup({ async: true });
						}
						$("#"+modalEle).spa_ARAccountPay({
							no:(param.no||""),//no
							mc:mc?mc:"",//名称
							title:title?title:"支付方式",//标题
						    price:param.price,//支付价钱
						    togatherPrice:param.togatherPrice,
						    nos : param.nos,
						    nos_checkout:param.nos_checkout,
						    bh : bh,
						    bhText : bhText,
						    operation:param.operation,
						    xhs:param.xhs,
							returnBack:function(){
								$("#"+modalEle).empty();
								$("#"+modalEle).modal("hide"); 
							},
							dataBack:function(data){
								if(data.morepayment=="NO"){
									$("#"+modalEle).modal("hide"); 
									$("#"+modalEle).empty();
									param.printBillBack();
									t.printBill(data);
								}else{
									$("#"+modalEle).empty();
									$("#"+modalEle).modal("hide"); 
									param.dataBack();
								}
							}
						});
					});
				}else if($(this).attr("data-paytype") == "RM"){//客账
					$(this).modalFrame("destroy");
					$(this).modalFrame("init",function(modalEle){   
						$("#"+modalEle).modal("show"); 
						if(!$.isFunction($().spa_guestAccountPay)){
							$.ajaxSetup({ async: false, cache: false });
				            $.getScript("js/cashier_mana/jquery.modal.spa_guestAccountPay.js");
				            $.ajaxSetup({ async: true });
						}
						$("#"+modalEle).spa_guestAccountPay({
							no:(param.no||""),//no
							mc:mc?mc:"",//名称
							bh:"887",//编号
							title:title?title:"支付方式",//标题
						    price:param.price,//支付价钱
						    togatherPrice:param.togatherPrice,
						    nos : param.nos,
						    nos_checkout:param.nos_checkout,
						    operation:param.operation,
						    xhs:param.xhs,
							returnBack:function(){
								$("#"+modalEle).empty();
								$("#"+modalEle).modal("hide"); 
							},
							dataBack:function(data){
								if(data.morepayment=="NO"){
									$("#"+modalEle).modal("hide"); 
									$("#"+modalEle).empty();
									param.printBillBack();
									t.printBill(data);
								}else{
									$("#"+modalEle).empty();
									$("#"+modalEle).modal("hide"); 
									param.dataBack();
								}
							}
						});
					});
				}else if($(this).attr("data-paytype") == "TR"){//余额转账
					console.log(303+":"+mc+"==="+$(this).attr("data-paytype"));
					$(this).modalFrame("destroy");
					$(this).modalFrame("init",function(modalEle){ 
						$("#"+modalEle).modal("show"); 
						if(!$.isFunction($().balance_transfer)){
							$.ajaxSetup({ async: false, cache: false });
				            $.getScript("js/cashier_mana/jquery.modal.balance_transfer.js");
				            $.ajaxSetup({ async: true });
						}
						$("#"+modalEle).balance_transfer({
							no:(param.no||""),//no
							mc:mc?mc:"",//名称
							bh:"887",//编号
							title:title?title:"支付方式",//标题
						    price:param.price,//支付价钱
						    togatherPrice:param.togatherPrice,
						    nos : param.nos,
						    nos_checkout:param.nos_checkout,
						    operation:param.operation,
						    xhs:param.xhs,
							returnBack:function(){
								$("#"+modalEle).empty();
								$("#"+modalEle).modal("hide"); 
							},
							dataBack:function(data){
								if(data.morepayment=="NO"){
									$("#"+modalEle).modal("hide"); 
									$("#"+modalEle).empty();
									param.printBillBack();
									t.printBill(data);
								}else{
									$("#"+modalEle).empty();
									$("#"+modalEle).modal("hide");
//									jf_initSettleAccounts();
									param.dataBack();
								}
							}
						});
					});
				}else{//XX结账方式
					$(this).modalFrame("destroy");
					$(this).modalFrame("init",function(modalEle){   
						$("#"+modalEle).modal("show"); 
						if(!$.isFunction($().spa_generalPay)){
							$.ajaxSetup({ async: false, cache: false });
				            $.getScript("js/cashier_mana/jQuery.modal.spa_generalPay.js");
				            $.ajaxSetup({ async: true });
						}
						$("#"+modalEle).spa_generalPay({
							no:(param.no||""),//no
							bh:bh?bh:"",//编号
							mc:mc?mc:"",//名称
							title:title?title:$.i18n.propJsp("支付方式"),//标题
						    price:param.price,//支付价钱
						    togatherPrice:param.togatherPrice,
						    bhText : bhText,
						    nos : param.nos,
						    nos_checkout:param.nos_checkout,
						    operation:param.operation,
						    xhs:param.xhs,
							returnBack:function(){
								$("#"+modalEle).empty();
								$("#"+modalEle).modal("hide");
								param.closeBack();
							},
							dataBack : function(data){
								if(data.morepayment=="NO"){
									$("#"+modalEle).modal("hide"); 
									$("#"+modalEle).empty();
									param.printBillBack();
									t.printBill(data);
								}else{
									$("#"+modalEle).empty();
									$("#"+modalEle).modal("hide");
									param.dataBack();
								}
							},
						});
					});
				}
			});
			
			//折扣
			el.find("#btnDiscount").unbind("click").click(function(){
				$(this).modalFrame("destroy");
				$(this).modalFrame("init",function(modalEle){   
					$("#"+modalEle).modal("show"); 
					if(!$.isFunction($().spa_discountDetail)){
						$.ajaxSetup({ async: false, cache: false });
			            $.getScript("js/cashier_mana/jquery.modal.spa_discountDetail.js");
			            $.ajaxSetup({ async: true });
					}
					$("#"+modalEle).spa_discountDetail({
						title:"折扣",//标题
					    no:(param.no||""),//no号
						closeBack:function(){
						},
						dataBack:function(){
							param.refreshBack();
						}
					});
				});
				
			});
			
			//折扣抹零
			el.find("#btnDiscountzero").unbind("click").click(function(){
				var json = param.getConnectInfo();
				param.togatherPrice = json.togatherPrice;
				console.log(param.togatherPrice)
				$(this).modalFrame("destroy");
				$(this).modalFrame("init",function(modalEle){   
					$("#"+modalEle).modal("show"); 
					if(!$.isFunction($().spa_discountZero)){
						$.ajaxSetup({ async: false, cache: false });
			            $.getScript("js/cashier_mana/jquery.modal.spa_discountZero.js");
			            $.ajaxSetup({ async: true });
					}
					$("#"+modalEle).spa_discountZero({
						title:"折扣抹零",//标题
						arg : "折扣抹零",
					    no:(param.no||""),//no号
						balance : param.netamt,
						togatherPrice : param.togatherPrice,
						closeBack:function(){
						},
						dataBack:function(){
							param.refreshBack();
						}
					});
				});
			});
			
			//服务费
			el.find("#btnService").unbind("click").click(function(){
				$(this).modalFrame("destroy");
				$(this).modalFrame("init",function(modalEle){   
					$("#"+modalEle).modal("show"); 
					if(!$.isFunction($().spa_fwf)){
						$.ajaxSetup({ async: false, cache: false });
			            $.getScript("js/cashier_mana/jquery.modal.spa_fwf.js");
			            $.ajaxSetup({ async: true });
					}
					$("#"+modalEle).spa_fwf({
						title:"服务费",//标题
					    no:(param.no||""),//no号
					    closeBack:function(){
						},
						dataBack:function(){
							param.refreshBack();
						}
					});
				});
			});

			//会员电子券
			el.find("#btnMemberCoupon").unbind("click").click(function(){
//				$(this).modalFrame("destroy");
//				$(this).modalFrame("init",function(modalEle){  
//					if(!$.isFunction($().coupon)){
//						$.ajaxSetup({async: false,cache: false });
//			            $.getScript(getContextPath()+"/static/hotel/frontoffice/js/coupon/jquery.modal.coupon2.js");
//			            $.ajaxSetup({async:true});
//					}
//					$("#"+modalEle).modal("show");
//					$("#"+modalEle).coupon({
//						no:param.no||"",
//						mno:param.mno||"", 
//						arg : "pos",
//						returnBack:function(){
//						},
//			    		dataBack:function(){
//			    			param.dataBack();
//			    		},
//					});
//				})
				
				if(!param.mno){
					jf_alert($.i18n.propJsp("此客人还没有绑定会员卡！！！"));
					return false;
				}
				$(this).modalFrame("destroy");
				$(this).modalFrame("init",function(modalEle){  
					if(!$.isFunction($().spa_use_coupon)){
						$.ajaxSetup({async: false,cache: false });
			            $.getScript("js/cashier_mana/jquery.modal.spa_use_coupon.js");
			            $.ajaxSetup({async:true});
					}
					$("#"+modalEle).modal("show");
					$("#"+modalEle).spa_use_coupon({
						no:param.no||"",
						mno:param.mno||"", 
						arg : "pos",
						urlPre : "/qms/mobile/",
						closeBack:function(){
						},
			    		dataBack:function(){
//			    			param.dataBack();
							param.refreshBack();
			    		},
					});
				})
			});
			
			//扫码电子券
			el.find("#btnScanCoupon").unbind("click").click(function(){
				$(this).modalFrame("destroy");
				$(this).modalFrame("init",function(modalEle){  
					if(!$.isFunction($().coupon_sweep_code)){
						$.ajaxSetup({async: false,cache: false });
			            $.getScript("js/cashier_mana/jquery.modal.coupon_sweep_code.js");
			            $.ajaxSetup({async:true});
					}
					$("#"+modalEle).modal("show");
					$("#"+modalEle).coupon_sweep_code({
						no:param.no||"",
						mno:param.mno||"", 
						arg : "pos",
						returnBack:function(){
						},
			    		dataBack:function(){
			    			param.refreshBack();
			    		},
					});
				})
			});	
			
			//使用优惠券  TODO已被 使用优惠券新 代替（备份）
			el.find("#btnUseEcoupon").unbind("click").click(function(){
				$(this).modalFrame("destroy");
				$(this).modalFrame("init",function(modalEle){  
					if(!$.isFunction($().spa_use_eCoupon)){
						$.ajaxSetup({async: false,cache: false });
			            $.getScript("js/cashier_mana/jquery.modal.spa_use_eCoupon.js");
			            $.ajaxSetup({async:true});
					}
					$("#"+modalEle).modal("show");
					$("#"+modalEle).spa_use_eCoupon({
						no:param.no||"",
						mno:param.mno||"", 
						arg : "pos",
						pageTh : param.pageTh,
						brzd_array : param.brzd_array,
						urlPre : "/qms/mobile/",
						bmh : param.bmh,
						returnBack:function(){
						},
			    		dataBack:function(){
			    			param.refreshBack();
			    		},
					});
				})
			});	
			
			//使用优惠券新 
			el.find("#btnUseEcoupon_new").unbind("click").click(function(){
				$(this).modalFrame("destroy");
				$(this).modalFrame("init",function(modalEle){  
					if(!$.isFunction($().spa_use_eCoupon_new)){
						$.ajaxSetup({async: false,cache: false });
			            $.getScript("js/cashier_mana/jquery.modal.spa_use_eCoupon_new.js");
			            $.ajaxSetup({async:true});
					}
					$("#"+modalEle).modal("show");
					$("#"+modalEle).spa_use_eCoupon_new({
						no:param.no||"",
						mno:param.mno||"", 
						arg : "pos",
						pageTh : param.pageTh,
						bmh : (param.bmh||""),
						brzd_array : param.brzd_array,
						closeBack:function(){
							param.refreshBack();
						},
			    		dataBack:function(){
			    			param.refreshBack();
			    		},
					});
				})
			});	
		},
		initFrameHtml : function(){
			
			var el = this.el;
			var param = this.param;
			var t = this;
 
			var eachWidth = parseInt(param.width/9);
			var otherWidth = param.width-eachWidth*8;
			param.eachWidth = eachWidth;
			param.otherWidth = otherWidth;
//			console.log("JSON::::"+JSON.stringify(param.initDs))
			var html = 	'<div style="display:flex;">'+
							'<div id="jz_left" class="widget-box no-padding" style="margin:0px;width:'+eachWidth*4+'px;">'+
								
							'</div>'+
							'<div id="jz_center" class="widget-box no-padding" style="margin:0px;width:'+otherWidth+'px;">'+
							
							'</div>'+
							'<div id="jz_right" class="widget-box no-padding" style="margin:0px;width:'+eachWidth*4+'px;">'+
								
							'</div>'+
						'</div>';
			el.html(html);
			var type = "";
			if(param.diffStyleSize<param.winWidth){
				type = "big";
			}else{
				type = "small";
			}
			this.initHtml(type);
		},
		initHtml : function(type){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var height = "0";
			var w = "";
			var fs = 12;
			var bh = "";
			var btnClass = "";
			var borderRadius = ""
			if(type=="big"){
				height = "102";
				w = param.eachWidth - 1 - 4;
				fs = param.bfs;
				bh = param.bh;
				btnClass = " btn-app btn-sm "
				borderRadius = "border-radius: 6px !important;";
			}else{
				height = "82"
				w = param.eachWidth - 1 - 14;
				fs = param.sfs;
				bh = param.sh;
			}
			//左侧HTML
			var lHtml = '<button id="btnDiscount" class="btn '+btnClass+' btn-grey" style="border:1px solid #FFF;height:'+bh+'px;width:'+(param.eachWidth-1)+'px;margin:0px;padding:6px 2px;'+borderRadius+'">'+
							'<span data-i18n="折扣" style="font-size:'+t.	getFontSize(w,fs,"折扣")+'px;">'+$.i18n.propJsp("折扣")+'</span>'+
						'</button>'+
						'<button id="btnDiscountzero" class="btn '+btnClass+' btn-grey" style="border:1px solid #FFF;height:'+bh+'px;width:'+(param.eachWidth-1)+'px;margin:0px;padding:6px 2px;'+borderRadius+'">'+
							'<span data-i18n="折扣抹零" style="font-size:'+t.getFontSize(w,fs,"折扣抹零")+'px;">'+$.i18n.propJsp("折扣抹零")+'</span>'+
						'</button>'+
						'<button id="btnService" class="btn '+btnClass+' btn-grey" style="border:1px solid #FFF;height:'+bh+'px;width:'+(param.eachWidth-1)+'px;margin:0px;padding:6px 2px;'+borderRadius+'">'+
							'<span data-i18n="服务费" style="font-size:'+t.getFontSize(w,fs,"服务费")+'px;">'+$.i18n.propJsp("服务费")+'</span>'+
						'</button>'+
						'<button id="btnUseEcoupon" class="btn '+btnClass+' btn-grey hide" style="border:1px solid #FFF;height:'+bh+'px;width:'+(param.eachWidth-1)+'px;margin:0px;padding:6px 2px;'+borderRadius+'">'+
							'<span data-i18n="使用优惠券" style="font-size:'+t.getFontSize(w,fs,"使用优惠券")+'px;">'+$.i18n.propJsp("使用优惠券")+'</span>'+
						'</button>'+
						'<button id="btnUseEcoupon_new" class="btn '+btnClass+' btn-grey" style="border:1px solid #FFF;height:'+bh+'px;width:'+(param.eachWidth-1)+'px;margin:0px;padding:6px 2px;'+borderRadius+'">'+
							'<span data-i18n="使用优惠券" style="font-size:'+t.getFontSize(w,fs,"使用优惠券")+'px;">'+$.i18n.propJsp("使用优惠券")+'</span>'+
						'</button>'+
						'<button id="btnMemberCoupon" class="btn '+btnClass+' btn-grey" style="border:1px solid #FFF;height:'+bh+'px;width:'+(param.eachWidth-1)+'px;margin:0px;padding:6px 2px;'+borderRadius+'">'+
							'<span data-i18n="会员电子券" style="font-size:'+t.getFontSize(w,fs,"会员电子券")+'px;">'+$.i18n.propJsp("会员电子券")+'</span>'+
						'</button>'+
						'<button id="btnScanCoupon" class="btn '+btnClass+' btn-grey" style="border:1px solid #FFF;height:'+bh+'px;width:'+(param.eachWidth-1)+'px;margin:0px;padding:6px 2px;'+borderRadius+'">'+
							'<span data-i18n="扫码电子券" style="font-size:'+t.getFontSize(w,fs,"扫码电子券")+'px;">'+$.i18n.propJsp("扫码电子券")+'</span>'+
						'</button>';
			el.find("#jz_left").html(lHtml);
			el.find("#jz_left").css({"height":height});
			
			//中间HTML
			var centerHtml ='<button id="btnChargeOff" type="button" class="btn '+btnClass+' btn-danger" style="height:'+bh+'px;width:'+(param.otherWidth-3)+'px;margin:1px;padding:6px 2px;">'+//background:#3a76b5 !important;opacity:0.8;
								'<span data-i18n="冲账" >'+$.i18n.propJsp("冲账")+'</span>'+
							'</button>'+
							'<button id="btnOther" class="btn '+btnClass+' btn-purple" style="border:1px solid #FFF;height:'+bh+'px;width:'+(param.otherWidth-3)+'px;margin:1px;padding:6px 2px;">'+//background:#3a76b5 !important;opacity:0.8;top:'+(parseInt(param.sh)+2)+'px;
								'<span data-i18n="更多" >'+$.i18n.propJsp("更多")+'</span>'+//style="font-size:'+t.getFontSize(w,param.sfz,"其他")+'px;"
							'</button>';
			el.find("#jz_center").html(centerHtml);
			el.find("#jz_center").css({"height":height});
			
			//右侧HTML
			var rHtml = t.getRHtml();
			el.find("#jz_right").html(rHtml);
			el.find("#jz_right").css({"height":height});
		},
		getRHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var html = "";
//			console.log("前::"+JSON.stringify(param.initDs));
			var arr = sortByKey(param.initDs,"sortQuick");
//			console.log("后::"+JSON.stringify(arr));
			$(arr).each(function(key,value){
				if(key<=7){
					var btn = '';
					if(param.diffStyleSize<param.winWidth){//大按钮 
						var fs = param.bfs; 
						var w = param.eachWidth-3-4;
						fs = t.getFontSize(w,fs,value.text||"");
						btn =	'<button class="btn btn-app btn-sm btn-primary paymenttype" data-code="'+value.dcode+'" data-paytype="'+ value.paytype +'" data-text="'+value.text+'" data-type="'+ value.crtype +'" id="pay'+ key +'" style="height:'+param.bh+'px;width:'+(param.eachWidth-3)+'px;margin:1px;padding:6px 2px;">'+
									'<span data-i18n="'+ value.text +'" style="font-size:'+fs+'px;">'+ $.i18n.propJsp(value.text||"") +'</span>'+
								'</button>';
					}else{//小按钮
						var fs = param.sfs; 
						var w = param.eachWidth-3-14;
						fs = t.getFontSize(w,fs,value.text||"");
						btn =	'<button class="btn btn-primary paymenttype" data-code="'+value.dcode+'" data-paytype="'+ value.paytype +'" data-text="'+value.text+'" data-type="'+ value.crtype +'" id="pay'+ key +'" style="height:'+param.sh+'px;width:'+(param.eachWidth-3)+'px;margin:1px;padding:6px 2px;">'+
									'<span data-i18n="'+ value.text +'" style="font-size:'+fs+'px;">'+ $.i18n.propJsp(value.text||"") +'</span>'+
								'</button>';
					}
					html += btn;
				}
			});
			return html;
		},
		getFontSize : function(width,l_fs,text){
			var el = this.el;
			var param = this.param;
			var t = this;
			var len = text.length;
			var fs = parseInt(width/len);
			if(fs>l_fs){
				fs = l_fs;
			}
			return fs;
		},
		printBill : function(data){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			el.find("#global_printBill").modalFrame("destroy");
			el.find("#global_printBill").modalFrame("init",function(modalEle){  
				$("#"+modalEle).modal("show");
		    	if(!$.isFunction($().ct_bill)){
					$.ajaxSetup({ async: false, cache: false });
		            $.getScript("js/cashier_mana/jquery.modal.ct_bill.js");
		            $.ajaxSetup({ async: true });
				}
				$("#"+modalEle).ct_bill({
					JSESSIONID : param.JSESSIONID,//"${JSESSIONID}",
					dbsql : "1",
					no : (data.no||""),
					close : true,
					billType:"BILL_SPA",//BILL_POS
					ls_param :{},
					dataBack :function(){
						
					},
					closeBack : function(){
						
					}
				});
			})
		}
	}
	
	function sortByKey(array, key) {
		return array.sort(function(a, b) {
			var x = parseInt(a[key]);
			var y = parseInt(b[key]);
			if(x==0){
				x = 9999;
			}
			if(y==0){
				y = 9999;
			}
			return ((x < y) ? -1 : ((x > y) ? 1 : 0));
		});
	}
	
})(jQuery);